#ifndef RENDERWIDGET_HPP
#define RENDERWIDGET_HPP


#include <GL/glew.h>

#include <QGLWidget>
#include <QTimer>
#include <QTime>
#include <BaseRender.hpp>
#include "TextRender.hpp"

namespace rtrt
{
  
  //! Abstract widget contains base features for rendering
  class BaseRenderWidget : public QGLWidget
  {
  Q_OBJECT
  public:
 
    //! Constructs widget for rendering with parent widget
    BaseRenderWidget( QWidget    *parent = 0  //!< A parent widget
                    );
    
    virtual ~BaseRenderWidget();
    
    //! Used rendering object
    virtual BaseRender* renderObject() = 0;
   
    //! Action sets using of stereo
    /*!
     * \sa BaseRender::StereoMode
     */
    QAction* useStereoModeAction();
    
    //! Action switches draw mode( using OpenGL or OpenCL )
    /*!
     *  \sa BaseRender::drawCL BaseRender::drawGL
     */
    QAction* useOpenCLModeAction();
    
    //! Action shows/hides fps in widget
    QAction* showFpsAction();
    //! Action shows/hides camera position and orientation in widget
    QAction* showCameraDataAction();
    //! Action chooses text's color for showing data in widget
    QAction* chooseTextColorAction();
  
    //! Current FPS for this widget
    GLfloat fps();
    
    //! Stops rendering process
    void stopRendering ();
    //! Starts rendering process
    void startRendering();
    
    //! Camera of current widget
    BaseCameraPtr camera();
    //! Sets camera to current widget
    void setCamera( BaseCameraPtr camera );

  protected:
    
    //! Initializes OpenGL and data linked with it
    void initializeGL (                       );
    //! Resizes widget
    void resizeGL     ( int width, int height );
    //! Redraws scene linked with widget
    void paintGL      (                       );

    //! Keyboard's events handler
    void keyPressEvent ( QKeyEvent *e //! Keyboard's event 
                       );
    
    //! Mouse's press event handler
    void mousePressEvent   ( QMouseEvent *e );
    //! Mouse's move event handler
    void mouseMoveEvent    ( QMouseEvent *e );
    //! Wheel's event handler
    void wheelEvent        ( QWheelEvent *e );

	QPoint _lastMousePos;

    
  private slots:
    
    void updateWindow();
    
    void useStereo( bool yes );
    void useOpenCL( bool yes );
    
    void showFPS       ( bool yes );
    void showCameraData( bool yes );
    
    void chooseTextColor();
    
  private:
    
    void createActions();
    
    void draw();
    
    static const int DEFAULT_X_SIZE = 256;
    static const int DEFAULT_Y_SIZE = 256;
    
    BaseCameraPtr _camera;
    
    QAction *_useStereoModeAction;
    QAction *_useOpenCLModeAction;
    
    QAction *_showFpsAction;
    QAction *_showCameraDataAction;
    QAction *_chooseTextColorAction;
    
    QTimer *_timer;
    
    void nextFrame();
    
    GLfloat _fps;
    
    GLint _frames;
    GLint _refreshTime;
    
    QTime* _workTime;
    QTime* _fpsTime;
    
    TextRender _text;
    
    bool _useStereo;
    
    bool _showFPS;
    bool _showCameraData; 
  };
  
}

#endif
